﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Media;

namespace BrainTechLLC.ImageOperatorsNonAForgeDependent
{
    public class BasicImageSourcePopulator
    {
        public virtual ImageSource Populate(BasicImageSource imageSource, ICoreDataProvider dataProvider, string fileName, BasicBitmapPopulator bitmapPopulator = null, BasicByteArrayPopulator byteArrayPopulator = null, int resizeWidth = 0, int resizeHeight = 0)
        {
            if (bitmapPopulator == null)
                bitmapPopulator = new BasicBitmapPopulator();

            Bitmap bitmap = bitmapPopulator.PopulateIfEmpty(imageSource, dataProvider, fileName, byteArrayPopulator);
            ImageSource populated = bitmap.CreateImageSourceFromBitmap(imageSource.Bytes, resizeWidth == 0 ? imageSource.BitmapWidth : resizeWidth, resizeHeight == 0 ? imageSource.BitmapHeight : resizeHeight);
            imageSource.ImageSource = populated;
            imageSource.ImageSourceWidth = populated == null ? 0D : populated.Width;
            imageSource.ImageSourceHeight = populated == null ? 0D : populated.Height;
            return populated;
        }

        public virtual ImageSource PopulateIfEmpty(BasicImageSource imageSource, ICoreDataProvider dataProvider, string fileName, BasicBitmapPopulator bitmapPopulator, BasicByteArrayPopulator byteArrayPopulator, int resizeWidth = 0, int resizeHeight = 0)
        {
            ImageSource populated = imageSource.ImageSource;
            if (populated == null)
            {
                populated = Populate(imageSource, dataProvider, fileName, bitmapPopulator, byteArrayPopulator, resizeWidth, resizeHeight);
            }
            return populated;
        }
    }
}
